package org.kjk.mail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

@Component("mail")
public abstract class Mail {
	protected String user;
	protected String pass;
	protected String receivingHost;
	protected Session session;

	public static Message[] message;
	public Store store;
	public Folder folder;

	// private int receivingPort;

	public Mail() {
		message = null;
		store = null;
		folder = null;
		user = null;
		pass = null;
	}

	public void setAccountDetails(String user, String pass) {
		this.user = user;// sender's email can also use as User Name
		this.pass = pass;
	}

	public class SMTPAuthenticator extends javax.mail.Authenticator {
		public String userName;
		public String passWord;

		public SMTPAuthenticator(String userName, String passWord) {
			this.userName = userName;
			this.passWord = passWord;
		}

		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(userName, passWord);
		}
	}

	public void closeMail() throws MessagingException {
		folder.close(true);
		store.close();
	}

	

	public List<MailDTO> getSubject() throws Exception {
		refreshData();
		List<MailDTO> result = new ArrayList<MailDTO>();
		try {
			if (message == null) {
				return null;
			}
			for (int i = 0; i < message.length; i++) {
				MailDTO dto = new MailDTO(message[i], i);
				result.add(dto);
			}
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return result;
	}

	public JSONObject getJsonSubject() throws Exception {
		List<MailDTO> list = getSubject();
		if (list != null) {
			JSONObject json = new JSONObject();
			JSONArray arr = new JSONArray();
			json.put("result", arr);
			int i = 0;
			for (MailDTO bean : list) {
				JSONObject obj = new JSONObject();
				obj.put("sender", bean.getSender().replace("\n", ""));
				obj.put("pageIndex", i++);
				obj.put("subject", bean.getSubject());
				obj.put("receivedDate", bean.getReceivedDate());
				obj.put("content", bean.getContent());
				arr.put(obj);
			}
			return json;
		} else {
			return null;
		}
	}

	public void refreshData() {
		if (message == null || message.length == 0) {
			readForce();
		}
	}

	public String getContent(int index) {
		refreshData();
		try {
			return message[index].getContent().toString();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return "fail to read";
	}

	public void deleteMessage(Message m) throws MessagingException {
		m.setFlag(Flags.Flag.DELETED, true);
		// if (m.isSet(Flags.Flag.DELETED))
		// System.out.println("DELETED message");
	}

	public abstract boolean postMail(String recipients[], String subject, String content);

	public abstract boolean sendHanmail(String to[], String subject, String content);

	public abstract void readForce();

	public void setUser(String user) {
		this.user = user;
	}

	public String getUser() {
		return user;
	}

}
